 

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import "package:get/get.dart";
import 'package:image_gallery_saver_plus/image_gallery_saver_plus.dart';
import 'package:dio/dio.dart';
import 'package:osg/controllers/http.dart';


// import 'package:flutter_flavorizr/flutter_flavorizr.dart';
 
//   import 'dart:html' as html;


// import 'package:http/http.dart' as http;
 
//  Future<void> saveImage(String imageUrl) async {
//   try {
//     // 获取图片数据
//     final response = await http.get(Uri.parse(imageUrl));
//     final bytes = response.bodyBytes;
//     final blob = html.Blob([Uint8List.fromList(bytes)]);
//     final url = html.Url.createObjectUrlFromBlob(blob);

//     // 创建隐藏的<a>标签并触发下载
//     final anchor = html.AnchorElement(href: url)
//       ..setAttribute('download', 'image.png')
//       ..style.display = 'none';
//     html.document.body?.children.add(anchor);
//     anchor.click();

//     // 清理
//     html.document.body?.children.remove(anchor);
//     html.Url.revokeObjectUrl(url);
//     Get.find<HTTPS>().showTost('保存成功');
//   } catch (e) {
//     print('保存图片失败: $e');
//   }
// }



class Poster extends StatefulWidget {
  Poster({super.key});

  @override
  State<Poster> createState() => _SettingPageState();
}

class _SettingPageState extends State<Poster> {

   _saveNetworkImage() async {
    var response = await Dio().get("$qr", options: Options(responseType: ResponseType.bytes));
    final result = await ImageGallerySaverPlus.saveImage(Uint8List.fromList(response.data), quality: 60, name: "hello");
    if(result['isSuccess']==true){
      Get.find<HTTPS>().showTost('保存成功');
    }else{
      Get.find<HTTPS>().showTost('保存失败');
    }
  }


  final qr = Get.parameters['qr'];
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {



    return Scaffold(
        backgroundColor: Color(0xffF1E3DA),
        appBar: AppBar(
          backgroundColor: Color.fromARGB(255, 249, 241, 236),
          centerTitle: true,
          leadingWidth: 40,
          leading: IconButton(
            onPressed: () {
              Get.back();
              print('object');
            },
            icon: Image.asset('static/images/back.png'),
          ),
        ),
        body: ListView(
          children: [
            FractionallySizedBox(
              widthFactor: 0.8,
              child: Image.network('$qr'),
            ),
            GestureDetector(
              onTap: () async {
                if (kIsWeb) {
                  // saveImage('$qr');
                } else {
                  _saveNetworkImage();
                }
              },
              child: Container(
                alignment: Alignment.center,
                margin: EdgeInsets.only(top: 0, left: 40, right: 40),
                decoration: BoxDecoration(
                    color: Color(0xff333333),
                    borderRadius: BorderRadius.circular(40)),
                width: 150,
                height: 50,
                child: Text(
                  '保存图片',
                  style: TextStyle(color: Color(0xffffffff), fontSize: 16),
                ),
              ),
            ),
          ],
        )

        // Align(
        //   alignment: Alignment.center,
        //   child: Column(
        //     children: [
        //       Text(
        //         '邀请好友加入OSG',
        //         style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
        //       ),
        //       Text(
        //         '每邀请一位好友注册并实名认证',
        //         style: TextStyle(fontSize: 12),
        //       ),
        //       Text(
        //         '可获得100星币',
        //         style: TextStyle(fontSize: 12),
        //       ),
        //       Image.asset('static/images/yaoqing.png',
        //           width: 270, fit: BoxFit.fitWidth),
        //       Container(
        //         margin: EdgeInsets.only(top:30),
        //         decoration: BoxDecoration(
        //             color: Color(0xff333333),
        //             borderRadius: BorderRadius.circular(40)),
        //         width: 150,
        //         height: 40,
        //         child: TextButton(
        //             onPressed: () => {},
        //             child: Text(
        //               '保存图片',
        //               style: TextStyle(color: Color(0xffffffff), fontSize: 16),
        //             )),
        //       ),
        //     ],
        //   ),
        // )
        );
  }
}
